package com.tju320.elm.view.impl;

import java.util.List;
import java.util.Scanner;

import com.tju320.elm.dao.FoodDao;
import com.tju320.elm.dao.impl.BusinessDaoImpl;
import com.tju320.elm.dao.impl.FoodDaoImpl;
import com.tju320.elm.po.Food;
import com.tju320.elm.view.FoodView;

public class FoodViewImpl implements FoodView{
	Scanner keyboardInput = new Scanner(System.in);
	@Override
	public List<Food> showFoodList(Integer businessId) {
		FoodDao foodDao=new FoodDaoImpl();
		List<Food> list=foodDao.listFoodByBusinessId(businessId);
		System.out.println("食品编号\t食品名称\t食品介绍\t食品价格");
		for(Food x:list) {
			System.out.printf("%d\t%s\t%s\t%.01f\n", x.getFoodId(),x.getFoodName(),x.getFoodExplain(),x.getFoodPrice());
		}
		return list;
	}
	

	@Override
	public void saveFood(Integer businessId) {
		Food food=new Food();
		food.setBusinessId(businessId);
		System.out.println("请输入食品名称:");
		food.setFoodName(keyboardInput.next());
		System.out.println("请输入食品介绍:");
		food.setFoodExplain(keyboardInput.next());
		System.out.println("请输入食品价格:");
		food.setFoodPrice(keyboardInput.nextDouble());
		FoodDao foodDao=new FoodDaoImpl();
		int key;
		if((key=foodDao.saveFood(food))>0) {
			System.out.printf("新增食品成功,食品编号为:%d\n",key);
		}else {
			System.out.println("操作失败");
		}
		
	}
	
	

	@Override
	public void updateFood(Integer businessId) {
		showFoodList(businessId);
		System.out.println("请输入你要更新的食品编号:");
		int foodId=keyboardInput.nextInt();
		FoodDao foodDao=new FoodDaoImpl();
		Food food=foodDao.getFoodById(foodId);
		System.out.printf("食品编号:%d\n食品名称:%s\n食品介绍:%s\n食品价格:%.1f\n所属商家:%d\n",
				food.getFoodId(),food.getFoodName(),food.getFoodExplain(),
				food.getFoodPrice(),food.getBusinessId());
		System.out.println("是否更新食品名称(y/n):");
		String s=keyboardInput.next();
		if(s.equals("y")) {
			System.out.println("请输入新的食品名称:");
			food.setFoodName(keyboardInput.next());
		}
		
		System.out.println("是否更新食品介绍(y/n):");
		s=keyboardInput.next();
		if(s.equals("y")) {
			System.out.println("请输入新的食品介绍:");
			food.setFoodExplain(keyboardInput.next());
		}
		
		System.out.println("是否更新食品价格(y/n):");
		s=keyboardInput.next();
		if(s.equals("y")) {
			System.out.println("请输入新的食品价格:");
			food.setFoodPrice(keyboardInput.nextDouble());
		}
		if(foodDao.updateFood(food)>0) {
			System.out.println("修改食品成功!");
		}else {
			System.out.println("修改失败!");
		}
		
	}
	public static void main(String[] args) {
		new FoodViewImpl().removeFood(1);
	}

	@Override
	public void removeFood(Integer businessId) {
		showFoodList(businessId);
		System.out.println("请选择要删除的食品编号:");
		int foodId=keyboardInput.nextInt();
		System.out.println("确认要删除吗（y/n）:");
		String s=keyboardInput.next();
		if(s.equals("y")) {
			FoodDao foodDao=new FoodDaoImpl();
			if(foodDao.removeFood(foodId)>0) {
				System.out.println("删除食品成功!");
			}else {
				System.out.println("删除失败!");
			}
		}else {
			System.out.println("你取消了这次操作!");
		}
		
	}

}
